package com.yntsoft.util;

import java.net.URL;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.Configuration;

public class EhcacheUtil {

	// ehcache配置文件地址
	private static String path = "ehcache.xml";

	private URL url;

	private CacheManager manager;

	private static EhcacheUtil ehCache;

	/**
	 * 带路径的缓存创建
	 * 
	 * @param path
	 */
	public EhcacheUtil(String path) {
		url = Configuration.class.getClassLoader().getResource(path);
		manager = CacheManager.create(url);
	}

	/**
	 * 获取换船实例
	 * 
	 * @return
	 */
	public static EhcacheUtil getInstance() {
		if (ehCache == null) {
			ehCache = new EhcacheUtil(path);
		}
		return ehCache;
	}

	/**
	 * 
	 * @param cacheName缓存名字
	 * @param key
	 *            键
	 * @param value
	 *            值
	 */
	public void put(String cacheName, String key, Object value) {
		Cache cache = manager.getCache(cacheName);
		Element element = new Element(key, value);
		if (cache == null) {
			manager.addCache(cacheName);
		}
		cache = manager.getCache(cacheName);
		cache.put(element);
	}

	/**
	 * 
	 * @param cacheName
	 *            缓存名字
	 * @param key
	 *            键
	 * @param value
	 *            数组形式的值
	 */
	public void put(String cacheName, String key, String[] value) {
		Cache cache = manager.getCache(cacheName);
		if (cache == null) {
			manager.addCache(cacheName);
		}
		cache = manager.getCache(cacheName);
		Element element = new Element(key, value);
		cache.put(element);
	}

	/**
	 * 替换缓存中的值
	 * 
	 * @param cacheName
	 * @param key
	 * @param value
	 */
	public void replace(String cacheName, String key, Object value) {
		Cache cache = manager.getCache(cacheName);
		Element element = new Element(key, value);
		cache.replace(element);
	}

	/**
	 * 通过缓存名称 和key 获取缓存中保持的值
	 * 
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public Object get(String cacheName, String key) {
		Cache cache = manager.getCache(cacheName);
		if (cache == null) {
			manager.addCache(cacheName);
			cache = manager.getCache(cacheName);
		}
		Element element = cache.get(key);
		return element == null ? null : element.getObjectValue();
	}

	/**
	 * 通过缓存名称 和key[] 集合获取多个缓存值
	 * 
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public Object get(String cacheName, String[] key) {
		Cache cache = manager.getCache(cacheName);
		if (cache == null) {
			manager.addCache(cacheName);
			cache = manager.getCache(cacheName);
		}
		Element element = cache.get(key);
		return element == null ? null : element.getObjectValue();
	}

	/**
	 * 获取缓存对象
	 * 
	 * @param cacheName
	 * @return
	 */
	public Cache get(String cacheName) {
		Cache cache = manager.getCache(cacheName);
		if (cache == null) {
			manager.addCache(cacheName);
			cache = manager.getCache(cacheName);
		}
		return cache;
	}

	/**
	 * 获取这个缓存中保存了多少个 key -value
	 * 
	 * @param cacheName
	 * @return
	 */
	public int getSize(String cacheName) {
		return manager.getCache(cacheName).getSize();
	}

	/**
	 * 通过缓存名称获取缓存保存的时间
	 * 
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public long getCreationTime(String cacheName, String key) {
		Cache cache = manager.getCache(cacheName);
		return cache.get(key).getCreationTime();
	}

	/**
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public long getCreationTime(String cacheName, String[] key) {
		Cache cache = manager.getCache(cacheName);
		return cache.get(key).getCreationTime();
	}

	/**
	 * 获取缓存的失效时间
	 * 
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public long getExpirationTime(String cacheName, String key) {
		Cache cache = manager.getCache(cacheName);
		return cache.get(key).getExpirationTime();
	}

	/**
	 * 获取缓存的失效时间
	 * 
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public long getExpirationTime(String cacheName, String[] key) {
		Cache cache = manager.getCache(cacheName);
		return cache.get(key).getExpirationTime();
	}

	/**
	 * 获取缓存最后更新时间
	 * 
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public long getLastUpdateTime(String cacheName, String key) {
		Cache cache = manager.getCache(cacheName);
		return cache.get(key).getLastUpdateTime();
	}

	/**
	 * 获取缓存最后更新时间
	 * 
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public long getLastUpdateTime(String cacheName, String[] key) {
		Cache cache = manager.getCache(cacheName);
		return cache.get(key).getLastUpdateTime();
	}

	/**
	 * 移除缓存中保存的值
	 * 
	 * @param cacheName
	 * @param key
	 */
	public void remove(String cacheName, String key) {
		Cache cache = manager.getCache(cacheName);
		cache.remove(key);
	}

	/**
	 * 移除缓存中保存的多个值
	 * 
	 * @param cacheName
	 * @param key
	 */
	public void remove(String cacheName, String[] key) {
		Cache cache = manager.getCache(cacheName);
		cache.remove(key);
	}
}
